package com.xs.shop.orders.service.controller;

import com.xs.shop.common.Result;
import com.xs.shop.orders.model.entity.OrderItem;
import com.xs.shop.orders.service.service.OrderItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs,zyx
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/orderItem")
@Tag(name = "订单项管理", description = "订单项的CRUD操作")
public class OrderItemController {

    @Autowired
    private OrderItemService orderItemService;

    @PostMapping
    @Operation(summary = "新增订单项", description = "创建一个新的订单项")
    public Result<OrderItem> addOrderItem(@RequestBody OrderItem orderItem) {
        OrderItem savedItem = orderItemService.addOrderItem(orderItem);
        return Result.success(savedItem);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除订单项", description = "根据ID删除订单项")
    public Result<Boolean> deleteOrderItem(@PathVariable @Parameter(description = "订单项ID") Long id) {
        boolean result = orderItemService.deleteOrderItem(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新订单项", description = "更新订单项信息")
    public Result<OrderItem> updateOrderItem(@RequestBody OrderItem orderItem) {
        OrderItem updatedItem = orderItemService.updateOrderItem(orderItem);
        return Result.success(updatedItem);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取订单项详情", description = "根据ID获取订单项详情")
    public Result<OrderItem> getOrderItemById(@PathVariable @Parameter(description = "订单项ID") Long id) {
        OrderItem orderItem = orderItemService.getOrderItemById(id);
        return Result.success(orderItem);
    }

    @GetMapping("/order/{orderId}")
    @Operation(summary = "获取订单项列表", description = "根据订单ID获取订单项列表")
    public Result<List<OrderItem>> getOrderItemsByOrderId(@PathVariable @Parameter(description = "订单ID") Long orderId) {
        List<OrderItem> items = orderItemService.getOrderItemsByOrderId(orderId);
        return Result.success(items);
    }
}
